/**
 * @file database.py
 * @brief Database connection and session management for Meeting Management System.
 * @author Ziyi Wang
 *
 * Provides SQLAlchemy engine, session, and Base for ORM models.
 */
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
import os
from dotenv import load_dotenv

load_dotenv()

SQLALCHEMY_DATABASE_URL = os.getenv("DATABASE_URL", "sqlite:///./meeting_system.db")

/**
 * @brief SQLAlchemy engine for database connection.
 */
engine = create_engine(
    SQLALCHEMY_DATABASE_URL, connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

/**
 * @brief Declarative base for ORM models.
 */
Base = declarative_base()

/**
 * @brief Dependency to get a database session.
 * @return SQLAlchemy session generator.
 */
def get_db():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close() 